package top.lyc829.mymusic.ui.fragment.play_list_detail

import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.map
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import com.lzx.starrysky.SongInfo
import com.lzx.starrysky.StarrySky
import top.lyc829.mymusic.R
import top.lyc829.mymusic.base.BaseFragment
import top.lyc829.mymusic.databinding.FragmentPlayListDetailBinding
import top.lyc829.mymusic.ext.*

class PlayListDetailFragment :
    BaseFragment<PlayListDetailViewModel, FragmentPlayListDetailBinding>() {

    companion object {
        fun newInstance() = PlayListDetailFragment()
        const val TAG = "PlayListDetailFragment"
    }

    override fun getViewModel() = ViewModelProvider(this).get(PlayListDetailViewModel::class.java)

    override fun getBinding(inflater: LayoutInflater) = FragmentPlayListDetailBinding.inflate(inflater)

    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.id.value = arguments?.getLong("id")
        mViewModel.copywriter.value = arguments?.getString("copywriter")
        binding.rvGedanDetailNormal.layoutManager = LinearLayoutManager(context)
        binding.rvGedanDetailNormal.adapter = mViewModel.adapter
    }

    override fun initClickListener() {
        mViewModel.adapter.setOnItemClickListener { _, _, position ->
            if (mViewModel.songs.value?.get(position)?.url.isNullOrEmpty()) {
                Toast.makeText(context, "该歌曲无法播放", Toast.LENGTH_SHORT).show()
                return@setOnItemClickListener
            }
            mViewModel.songs.value?.let { list ->
                eventViewModel.refreshPlayList(list.map { it.convertToSongInfo() })
                StarrySky.with().playMusicById(list[position].id.toString())
            }
        }

        binding.llPlayHeader.setOnClickListener {
            eventViewModel.playByListAndIndex(mViewModel.songs.value?.map { it.convertToSongInfo() }, 0)
        }

        binding.llGedanSubscribe.setOnClickListener {

        }

        binding.bottomMusicView.setOnClickListener {
            findNavController().navigate(R.id.action_playListDetailFragment_to_musicPlayerFragment)
        }

        binding.llGedanDetailComment.setOnClickListener {
            val id: Long = mViewModel.id.value ?: -1
            val bundle = Bundle()
            bundle.putLong("id", id)
            findNavController().navigate(R.id.action_playListDetailFragment_to_commentFragment, bundle)
        }

    }

    override fun createObserver() {

        mViewModel.copywriter.observe(viewLifecycleOwner) {
            binding.tvGedanDetailToolbarReason.text = it
        }

        mViewModel.playlistDetail.observe(viewLifecycleOwner) {
            binding.tvGedanDetailTitle.text = it.name
            binding.ivGedanDetailAvatarImg.loadAsCircle(it.creator.avatarUrl)
            binding.ivGedanDetailImg.load(it.coverImgUrl)
            binding.tvGedanDetailAvatarName.text = it.creator.nickname
            binding.tvGedanDetailPlaynum.text = it.playCount.getCorrespondString
            binding.tvGedanDetailDesc.text = it.description

            binding.ivGedanDetailImgBack.loadWithBlur(it.coverImgUrl, 150)
            binding.tvGedanDetailCommentCount.text = it.commentCount.getCorrespondString
            binding.tvGedanDetailShareCount.text = it.shareCount.getCorrespondString
            binding.tvGedanDetailSongNum.text = "(共" + it.trackCount.toString() + "首)"
            binding.tvGedanDetailCollectCount.text = "收藏 (" + it.subscribedCount.getCorrespondString + ")"

        }
        mViewModel.songs.observe(viewLifecycleOwner) {
            mViewModel.adapter.setList(it)
        }

        mViewModel.isLoading.observe(viewLifecycleOwner) {
            if (it) {
                binding.normalLoading.root.visibility = View.VISIBLE
                binding.rvGedanDetailNormal.visibility = View.INVISIBLE
            } else {
                binding.normalLoading.root.visibility = View.GONE
                binding.rvGedanDetailNormal.visibility = View.VISIBLE
            }
        }
    }

    override fun initData() {
        //拿到id去请求歌单
        mViewModel.id.observe(viewLifecycleOwner) {
            mViewModel.getPlayListDetail(it)
        }

    }

}